package com.dsg.controller.coupon;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.coupon.DsgCouponSend;
import com.dsg.entity.dto.CouponSendDto;
import com.dsg.service.coupon.IDsgCouponSendService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 派发油券资料 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/coupon/dsgCouponSend")
public class DsgCouponSendController extends BaseController {
    @Autowired
    private IDsgCouponSendService service;
    @Autowired
    ObjectMapper objectMapper;

    @GetMapping(value = "/selectCouponList")
    @ApiOperation("油券管理")
    @ApiResponses(
            @ApiResponse(response = CouponSendDto.class, code = 0, message = "ok")
    )
    public AjaxResult selectCouponList(String cusNo, Integer appType, Integer isSend) {
        List<CouponSendDto> list = service.selectCouponList(cusNo, appType, isSend, getEntCode());
        return success(list);
    }

    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgCouponSend couponSend) {
        couponSend.setOid(UUIDGenerator.getUUID());
        boolean falg = service.save(couponSend);
        if (falg) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @PostMapping(value = "/distribute")
    @ApiOperation("油券管理-派發")
    public AjaxResult distribute(String oid) {
        DsgCouponSend couponSend = service.getById(oid);
        if (couponSend.getReaSendTime() != null) {
            return fail("已派發，不能重覆派發");
        }

        couponSend.setReaSendTime(new Date());
        service.updateById(couponSend);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgCouponSend couponSend) {
        boolean flag = service.updateById(couponSend);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, couponSend);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public AjaxResult delete(String id) {
        Boolean flag = service.removeById(id);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgCouponSend
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgCouponSend dsgCouponSend) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("a.CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgCouponSend.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgCouponSend);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }
}
